package com.changpay.zywy.ui.activity.main

import android.app.DatePickerDialog
import android.content.Intent
import android.support.v4.app.FragmentTransaction
import android.util.Log
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseMvpActivity
import com.changpay.zywy.bean.EarningTotalDefaultBean
import com.changpay.zywy.bean.WalletInfoBean
import com.changpay.zywy.constant.Constant
import com.changpay.zywy.mvp.contract.IncomeContract
import com.changpay.zywy.mvp.presenter.IncomePresenter
import com.changpay.zywy.ui.activity.mine.BankCardActivity
import com.changpay.zywy.ui.activity.mine.IncomeDetailActivity
import com.changpay.zywy.ui.fragment.main.IncomeGroupFragment
import com.changpay.zywy.ui.fragment.main.IncomePersonFragment
import com.changpay.zywy.ui.fragment.main.IncomeTotalFragment
import com.changpay.zywy.utils.DialogUtil
import com.changpay.zywy.utils.Preference
import kale.ui.view.dialog.EasyDialog
import kotlinx.android.synthetic.main.activity_income.*
import kotlinx.android.synthetic.main.title_right_layout.*
import java.util.*

class IncomeActivity : BaseMvpActivity<IncomeContract.View, IncomeContract.Presenter>(), IncomeContract.View {
    override fun showWalletInfo(data: WalletInfoBean) {
        tv_freeze.text = "已冻结¥" + (data?.freeze ?: "")
        tv_balance.text = data.available ?: ""
        tv_freeze_two.text = "已冻结¥" + (data?.freeze_two ?: "")
        tv_balance_two.text = data.available_two ?: ""
    }


    private val INCOME_TYPE_INDEX: String = "type_index"
    private val FRAGMENT_TOTAL = 0x01
    private val FRAGMENT_PERSION = 0x02
    private val FRAGMENT_GROUP = 0x03
    private var mIndex = FRAGMENT_TOTAL

    private var mTotalFragment: IncomeTotalFragment? = null
    private var mPersonFragment: IncomePersonFragment? = null
    private var mGroupFragment: IncomeGroupFragment? = null


    private lateinit var onDateSetListener: DatePickerDialog.OnDateSetListener

    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.tv_check_time -> onDateSetListener.let {
                DialogUtil.showDatePickerDialog(
                    this,
                    it,
                    Calendar.getInstance()
                )
            }
            R.id.iv_back -> finish()
            R.id.tv_income_detail_one -> Intent(this, IncomeDetailActivity::class.java).run {
                putExtra("wallet_type", "1")
                startActivity(this)
            }
            R.id.tv_income_detail_two -> Intent(this, IncomeDetailActivity::class.java).run {
                putExtra("wallet_type", "2")
                startActivity(this)
            }
            R.id.tv_ti_xian -> Intent(this, TiXianActivity::class.java).run {
                putExtra("yu_er", tv_balance.text.toString())
                putExtra("wallet_type", "1")
                startActivity(this)
            }
            R.id.tv_ti_xian_two -> Intent(this, TiXianActivity::class.java).run {
                putExtra("yu_er", tv_balance_two.text.toString())
                putExtra("wallet_type", "2")
                startActivity(this)
            }
        }
    }
    /**
     * local password
     */
    private var bank: Int by Preference(Constant.IS_BANE, 0)

    override fun attachLayoutRes(): Int = R.layout.activity_income

    override fun initData() {
    }

    override fun initView() {
        super.initView()
        tv_title.text = "我的余额"
        tv_right.text = ""
        tv_ti_xian.setOnClickListener(onClickListener)
        tv_check_time.setOnClickListener(onClickListener)
        tv_right.setOnClickListener(onClickListener)
        iv_back.setOnClickListener(onClickListener)
        tv_ti_xian_two.setOnClickListener(onClickListener)
        tv_income_detail_one.setOnClickListener(onClickListener)
        tv_income_detail_two.setOnClickListener(onClickListener)
        onDateSetListener = DatePickerDialog.OnDateSetListener { datePicker, year, month, day ->
            val select_date = year.toString() + "-" + (month + 1) + "-" + day
            Log.e("mango", "onDateSet: $select_date")
            when (mIndex) {
                FRAGMENT_TOTAL -> mTotalFragment?.setTime(select_date)
                FRAGMENT_PERSION -> mPersonFragment?.setTime(select_date)
                FRAGMENT_GROUP -> mGroupFragment?.setTime(select_date)
            }
        }
        income_type_rg.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.rb_total_income -> showFragment(FRAGMENT_TOTAL)
                R.id.rb_personal_income -> showFragment(FRAGMENT_PERSION)
                R.id.rb_group_income -> showFragment(FRAGMENT_GROUP)
            }
        }
        showFragment(mIndex)
    }


    override fun start() {


    }

    override fun showEarning(data: EarningTotalDefaultBean) {
        tv_freeze.text = "已冻结¥" + (data.price?.freeze ?: "")
        tv_balance.text = data.price?.available ?: ""
    }

    override fun showDefaultMsg(msg: String) {
        super.showDefaultMsg(msg)
        if (bank == 0) {
            val builder = EasyDialog.builder(this) // 建立builder对象
            builder.setTitle("提示")
                .setMessage("请先绑定银行卡")
                // 设置下方的三个按钮
                .setPositiveButton(
                    "确定"
                ) { _, _ ->
                    startActivity(BankCardActivity::class.java)
                    finish()
                }
                .setCancelable(false) // 点击空白处可以关闭
            val easyDialog = builder.build<EasyDialog>()
            easyDialog.show(supportFragmentManager, "")
        }
    }

    override fun createPresenter(): IncomeContract.Presenter = IncomePresenter()


    /**
     * 隐藏所有的Fragment
     */
    private fun hideFragments(transaction: FragmentTransaction) {
        mTotalFragment?.let { transaction.hide(it) }
        mPersonFragment?.let { transaction.hide(it) }
        mGroupFragment?.let { transaction.hide(it) }
    }

    /**
     * 展示Fragment
     * @param index
     */
    private fun showFragment(index: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        hideFragments(transaction)
        mIndex = index
        when (index) {
            FRAGMENT_TOTAL // 总收益
            -> {
                if (mTotalFragment == null) {
                    mTotalFragment = IncomeTotalFragment.getInstance()
                    transaction.add(R.id.container, mTotalFragment!!, "total")
                } else {
                    transaction.show(mTotalFragment!!)
                }
            }
            FRAGMENT_PERSION // 个人收益
            -> {
                if (mPersonFragment == null) {
                    mPersonFragment = IncomePersonFragment.getInstance()
                    transaction.add(R.id.container, mPersonFragment!!, "person")
                } else {
                    transaction.show(mPersonFragment!!)
                }
            }
            FRAGMENT_GROUP // 团队收益
            -> {
                if (mGroupFragment == null) {
                    mGroupFragment = IncomeGroupFragment.getInstance()
                    transaction.add(R.id.container, mGroupFragment!!, "group")
                } else {
                    transaction.show(mGroupFragment!!)
                }
            }
        }
        transaction.commit()
    }

    override fun onResume() {
        super.onResume()
        val param = hashMapOf<String, String>()
        param["key"] = token
//        mPresenter?.getEarning(param)
        mPresenter?.getWalletInfo(param)
    }
}
